#include <stdio.h>
#include <stdlib.h>
#include "sensor_data.pb-c.h"

// 模拟 GPRS 接收函数
void receive_gprs(uint8_t *buffer, size_t len) {
    Sensor__SensorDataList *msg_list;

    // 反序列化
    msg_list = sensor__sensor_data_list__unpack(NULL, len, buffer);
    if (msg_list == NULL) {
        fprintf(stderr, "Failed to unpack message\n");
        return;
    }

    // 遍历并打印数据
    printf("Received %zu sensor data items:\n", msg_list->n_items);
    for (size_t i = 0; i < msg_list->n_items; i++) {
        Sensor__UnifiedSensorData *data = msg_list->items[i];
        printf("Item %zu:\n", i + 1);
        printf("  ID: %d\n", data->id);
        printf("  Name: %s\n", data->name);

        switch (data->sensor_value_case) {
            case SENSOR__UNIFIED_SENSOR_DATA__SENSOR_VALUE_TEMPERATURE_DATA:
                printf("  Temperature: %.1f\n", data->temperature_data->temperature);
                break;
            case SENSOR__UNIFIED_SENSOR_DATA__SENSOR_VALUE_HUMIDITY_DATA:
                printf("  Humidity: %.1f\n", data->humidity_data->humidity);
                break;
            default:
                printf("  Unknown sensor type\n");
        }
    }

    // 清理内存
    sensor__sensor_data_list__free_unpacked(msg_list, NULL);
}

int main() {
    // 模拟接收到的二进制数据（从 sender_unified.c 输出获取）
	uint8_t sample_data[] = {
		0x0a, 0x15, 0x08, 0x65, 0x12, 0x0a, 0x54, 0x65, 0x6d, 0x70, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 
		0x1a, 0x05, 0x0d, 0x00, 0x00, 0xcc, 0x41, 0x0a, 0x16, 0x08, 0x66, 0x12, 0x0b, 0x48, 0x75, 0x6d, 
		0x69, 0x64, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x22, 0x05, 0x0d, 0x00, 0x00, 0x70, 0x42, 
	};
    size_t len = sizeof(sample_data);

    receive_gprs(sample_data, len);
    return 0;
}